package com.swak.generate.api;

import java.util.List;

import com.swak.doc.Api;
import com.swak.generate.api.builder.ApiBuilder;
import com.swak.utils.Lists;

/**
 * 配置项目
 *
 * @author: lifeng
 * @date: 2020/3/28 15:11
 */
public class ApiConfig {

	public static ApiConfig of() {
		return new ApiConfig();
	}

	private List<String> sourcePaths;
	private List<String> packages;

	/**
	 * 创建 Api Doc
	 *
	 * @return apis
	 */
	public List<Api> build() {
		return new ApiBuilder(this).build();
	}

	/**
	 * 添加源码路径
	 *
	 * @param sourcePath 源码文件路径
	 * @return 配置
	 */
	public ApiConfig addSourcePath(String sourcePath) {
		if (sourcePaths == null) {
			sourcePaths = Lists.newArrayList();
		}
		sourcePaths.add(sourcePath);
		return this;
	}

	/**
	 * 是否符合路径
	 *
	 * @param javaFile java文件
	 * @return 是否符合包路径规则
	 */
	public boolean isPackageMatch(String javaFile) {
		if (packages != null && packages.size() > 0) {
			for (String str : packages) {
				if (str.endsWith("*")) {
					String name = str.substring(0, str.length() - 2);
					if (javaFile.contains(name)) {
						return true;
					}
				} else {
					if (javaFile.contains(str)) {
						return true;
					}
				}
			}
			return false;
		}
		return true;
	}

	public List<String> getSourcePaths() {
		return sourcePaths;
	}

	public ApiConfig setSourcePaths(List<String> sourcePaths) {
		this.sourcePaths = sourcePaths;
		return this;
	}

	public List<String> getPackages() {
		return packages;
	}

	public ApiConfig setPackages(List<String> packages) {
		this.packages = packages;
		return this;
	}
}
